
//
//  NetDataEngine.m
//  Trace
//
//  Created by qianfeng on 16/3/13.
//  Copyright © 2016年 王卓越. All rights reserved.
//

#import "NetDataEngine.h"
#import "AFNetworking.h"


@implementation NetDataEngine

+ (instancetype)sharedInstance{
    static NetDataEngine *s_instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        s_instance = [[NetDataEngine alloc] init];
        
    });
    return s_instance;
}

- (NSString *)urlEncoded:(NSString *)urlInput{
    //为了防止出现由特殊字符引起的错误，进行如下处理
   
    return [urlInput stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
}

- (void)GET:(NSString *)url success:(SuccessBlockType)successBlock failed:(FailedBlockType)failedBlock{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //补充 可接受文件类型
    NSSet *set = manager.responseSerializer.acceptableContentTypes;
    NSMutableSet *acceptSet = [NSMutableSet setWithSet:set];

    [acceptSet addObject:@"text/html"];
    manager.responseSerializer.acceptableContentTypes = acceptSet;
    
    NSString *urlEncoded = [self urlEncoded:url];
    [manager GET:urlEncoded parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        if (successBlock) {
            successBlock(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failedBlock) {
            failedBlock(error);
        }
    }];
    
}

- (void)requestFirstPageUrl:(NSString *)url success:(SuccessBlockType)successBlock failed:(FailedBlockType)failedBlock{
   [self GET:url success:successBlock failed:failedBlock];

}


- (void)requestDiscoverUrl:(NSString *)url success:(SuccessBlockType)successBlock failed:(FailedBlockType)failedBlock{
    [self GET:url success:successBlock failed:failedBlock];
    
}

- (void)requestAnchorUrl:(NSString *)url success:(SuccessBlockType)successBlock failed:(FailedBlockType)failedBlock{
    [self GET:url success:successBlock failed:failedBlock];
}

@end
